package com.hankol.community.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hankol.community.R;
import com.hankol.community.adapter.TopicChannelAdapter;

import java.util.ArrayList;

/**
 * @author:hanke 2022/3/30
 * desc:
 */
public class PopWindowUtils {
    public static PopupWindow showTopicChannel(Activity activity, ArrayList<String> channels, TopicChannelAdapter.SelectChannelListener selectChannelListener) {
        View contentView = LayoutInflater.from(activity).inflate(R.layout.layout_topic_channel_pop, null);
        LinearLayout llArrowUp = contentView.findViewById(R.id.ll_arrow_up);
        RecyclerView recyclerView = contentView.findViewById(R.id.rv_my_topic_channel);
        TopicChannelAdapter adapter = new TopicChannelAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setNewData(channels);

        PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //点击外部消失
        popupWindow.setOutsideTouchable(false);
        //设置可以点击
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);// 取得焦点
//        setBackgroundAlpha(activity,0.5f);
        //进入退出的动画，指定刚才定义的style
//        popupWindow.setAnimationStyle(R.style.camera_popwindow_anim_style);
        popupWindow.showAtLocation(contentView, Gravity.TOP, 0, 0);
        adapter.setSelectChannelListener(position -> {
            selectChannelListener.onSelectChannel(position);
            popupWindow.dismiss();
        });
        llArrowUp.setOnClickListener(v -> popupWindow.dismiss());
        contentView.findViewById(R.id.view_background).setOnClickListener(v -> popupWindow.dismiss());
        return popupWindow;
    }

    public static PopupWindow showPopwindow(final Activity activity, View contentView, int width, int height) {
        PopupWindow popupWindow = new PopupWindow(contentView, width, height);
        popupWindow.setFocusable(true);// 取得焦点
        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //点击外部消失
        popupWindow.setOutsideTouchable(true);
        //设置可以点击
        popupWindow.setTouchable(true);
        setBackgroundAlpha(activity, 0.5f);//设置屏幕透明度
        //进入退出的动画，指定刚才定义的style
        popupWindow.setAnimationStyle(R.style.camera_popwindow_anim_style);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(activity, 1.0f);
            }
        });
        return popupWindow;
    }

    public static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }


}
